package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.io.PrintWriter;
import java.sql.SQLException;
import bean.ConnectMySql;
import bean.Login;
/**
 * Servlet implementation class login
 */
@WebServlet("/loginUser")
public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  PreparedStatement st=null;
		  ResultSet rs = null;
	      Connection con=ConnectMySql.getConnection();
	      request.setCharacterEncoding("UTF-8");
	      String user=request.getParameter("user");
	      String pwd=request.getParameter("pwd");
		  try {
			    //用户查询
			    request.setCharacterEncoding("UTF-8");
	            st=con.prepareStatement("select * from login where name=? and password=?");
	            st.setString(1,user);
	            st.setString(2,pwd);
	            rs=st.executeQuery();
	            PrintWriter out=response.getWriter();
	            Login l=new Login();
	            if(rs.next()){
	            	l.setId(rs.getInt(1));
	            	l.setName(rs.getString(2));
	            	l.setPassword(rs.getString(3));
	            	l.setPhone(rs.getString(4));
	            	l.setAddress(rs.getString(5));
	            	HttpSession session=request.getSession();
	            	session.setAttribute("userTotal", l);
	            	session.setAttribute("user", user);
	            	session.setAttribute("pwd",pwd);
	            	session.setAttribute("xxx",String.valueOf(rs.getInt(1)));
	            }
	            //管理员查询
	            st=con.prepareStatement("select * from admin where adminName=? and adminPassword=?");
	            st.setString(1,user);
	            st.setString(2,pwd);
	            rs=st.executeQuery();
	            if(rs.next()){
	            	HttpSession session=request.getSession();
	            	session.setAttribute("admin", user);
	            	session.setAttribute("adminpwd",pwd);
	            	session.setAttribute("adminId",String.valueOf(rs.getInt(1)));
	            }
	            ConnectMySql.closeResultSet(rs);
	            ConnectMySql.closeStatement(st);
	  		    ConnectMySql.closeConnection(con);
			}catch(SQLException e) {
				e.printStackTrace();
			}
		    response.sendRedirect("login.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
